#martix nxm
def decoding(martix):
    n= len(martix)
    m= len(martix[0])
    num = list(range(n))
    code = []
    for ite in range(n-2):
        i = min(num)
        print(i)
        print(num)
        for index,j in enumerate(martix[i]):
            if j == 1 :
                if i in num: 
                    num.remove(i)
                if j in num:
                    num.remove(j)
                code.append(index+1)
                #print(j)
                print(i,index)
                break
    return code

def encoding(code):
    num = list(range(1,len(code)+2+1))
    P = list(code)
    P_ = list(set(num) - set(code))
    shape = len(code)+2
    martix = [[0 for i in range(shape)] for i in range(shape)]
    while not (len(P) == 0 and len(P_) == 2):
        print(P)
        print(P_)
        print(martix)
        i = min(P_)
        j = P[0]
        print(i,j)
        martix[i-1][j-1] = 1
        P_.remove(i)
        P.remove(j)
        if j not in P:
            P_.append(j)
    martix[P_[0]-1][P_[1]-1] = 1
    return martix
        
'''
matrix = [[0, 1, 0,0,0,0],
        [1,0,1,0,1,1],
        [0,1,0,0,0,0],
        [0,0,0,0,0,1],
        [0,1,0,0,0,0],
        [0,1,0,1,0,0]]
'''
matrix= [[0,1,0,0,0,0,0],
        [1,0,1,1,0,0,0],
        [0,1,0,0,1,0,0],
        [0,1,0,0,0,1,1],
        [0,0,1,0,0,0,0],
        [0,0,0,1,0,0,0],
        [0,0,0,1,0,0,0]]
print(decoding(matrix))
code =[6,3,2,4,4]
#print(code)
print(encoding(code))
